﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace UnDbxNET.Files
{
    class dbxSys
    {
        public static Int64 sys_fread_int64(Stream pStream)
        {
            Int64 pValue;

#if WORDS_BIGENDIAN
                   /* the following code is endianness neutral */
                Int64 llw = 0;
                llw = (Int64)(pStream.ReadByte() & 0xFF);
                llw |= ((Int64)(pStream.ReadByte() & 0xFF) << 0x08);
                llw |= ((Int64)(pStream.ReadByte() & 0xFF) << 0x10);
                llw |= ((Int64)(pStream.ReadByte() & 0xFF) << 0x18);
                llw |= ((Int64)(pStream.ReadByte() & 0xFF) << 0x20);
                llw |= ((Int64)(pStream.ReadByte() & 0xFF) << 0x28);
                llw |= ((Int64)(pStream.ReadByte() & 0xFF) << 0x30);
                llw |= ((Int64)(pStream.ReadByte() & 0xFF) << 0x38);
                pValue = llw;
#else
            Byte[] MyByte = new Byte[sizeof(Int64)];
            pStream.Read(MyByte, 0, sizeof(Int64));
            pValue = BitConverter.ToInt64(MyByte, 0);
#endif
            return pValue;
        }

        public static int sys_fread_int(Stream pStream)
        {
            int pValue;
#if WORDS_BIGENDIAN
              /* the following code is endianness neutral */
              int dw = 0;
              dw =  (int) (pStream.ReadByte() & 0xFF);
              dw |= ((int) (pStream.ReadByte() << 0x08);
              dw |= ((int) (pStream.ReadByte() << 0x10);
              dw |= ((int) (pStream.ReadByte() << 0x18);
              pValue = dw;
#else
            Byte[] MyByte = new Byte[sizeof(int)];
            pStream.Read(MyByte, 0, sizeof(int));
            pValue = BitConverter.ToInt32(MyByte, 0);
#endif
            return pValue;
        }

        public static short sys_fread_short(Stream pStream)
        {
            short pValue;
#if WORDS_BIGENDIAN
          /* the following code is endianness neutral */
          short w = 0;
          w =  (short) (pStream.ReadByte() & 0xFF);
          w |= ((short) (pStream.ReadByte() & 0xFF) << 0x08);
          pValue = w;
#else
            Byte[] MyByte = new Byte[sizeof(short)];
            pStream.Read(MyByte, 0, sizeof(short));
            pValue = BitConverter.ToInt16(MyByte, 0);
#endif
            return pValue;
        }
    }
}
